package com.atguigu.flink.function;

import com.atguigu.flink.pojo.WaterSensor;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Smexy on 2023/2/27
 */
public class MyUtil
{
    //把Iterable<WaterSensor>转为 List
    public static <T> List<T>  parseToList(Iterable<T> elements){

    List<T> result =new ArrayList<>();

    for (T element : elements) {
        result.add(element);
    }

    return result;

    }

    private  static DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");


    /*
        把TimeWindow解析为具体的时间

        处理时间的api，都位于 java.time包下
     */
    public static String parseTimeWindow(TimeWindow w){

        LocalDateTime startTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(w.getStart()), ZoneId.of("Asia/Shanghai"));
        LocalDateTime endTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(w.getEnd()), ZoneId.of("Asia/Shanghai"));

        //用格式对时间进行格式化
        return "[" + startTime.format(timeFormatter) +"," + endTime.format(timeFormatter) +")";

    }
}
